<?php

namespace Quiz\Controller;
use Think\Controller;

class CustomQuizController extends Controller {

    /**
     * 计算问卷
     * @param $uid
     * @return bool
     * @throws \Exception
     */

    public function calcAnswer($quiz_id, $uid)
    {

        if ($quiz_id == 14){
            $this->tmp_disc_result($uid);
            return true;
        }

        $quiz_name = M("quiz_info")->where(['quiz_id' => $quiz_id])->find();
        $quiz_name = $quiz_name['quiz_name'];

        $url = "http://localhost:8090/demo/Main";

        $data['action'] = "calc";
        $data['userid'] = $uid;
        //$data['quiz_id'] = $quiz_id;
        $data['custom_quizname'] = $quiz_name;
        $sex = M('users')->where(['uid' => $uid])->select();
        $sex = $sex[0]['sex'];
        if($sex == 'male')
            $data['sex'] = "男";
        else
            $data['sex'] = "女";

        $httpstr = http($url, $data, 'GET', array("Content-type: text/html; charset=utf-8"));//调用计算部分java

        if ($httpstr)
            return true;
        else
            return false;
    }

    //临时随机生成disc答案
    public function tmp_disc_result($uid)
    {
        for($i = 0; $i < 4; $i++){
            $data[$i]['user_id'] = $uid;
            $data[$i]['divisor_level'] = 1;
            $data[$i]['element_index'] = $i + 1;
            $data[$i]['row_score'] = rand(0,40);
            $data[$i]['trans_score'] = -1;
            $data[$i]['quiz_id'] = 14;
        }
         M("calc_result")->addAll($data);

    }


    /**
     * 自定义问卷，显示count方法
     * @param $quiz_id
     * @param $uid
     */

    public function display_count($quiz_id, $uid)
    {
        $quiz_info = M("quiz_info")->where(['quiz_id' =>$quiz_id])->find();

        $user_result = M("calc_result")->join("element_extend on element_extend.quiz_id = calc_result.quiz_id and element_extend.divisor_level = calc_result.divisor_level and element_extend.element_index = calc_result.element_index")
            ->where(['calc_result.quiz_id' => $quiz_id, 'calc_result.user_id'=>$uid])
            ->select();



        //var_dump($user_result);
        $this->assign("quiz_info", $quiz_info);
        $this->assign("user_result", $user_result);
        $user_info = M("users")->where(['uid'=>$uid])->find();
        $this->assign("userinfo", $user_info);
        $this->display("Quiz@CustomQuiz:display_count");
    }


    /**
     * 自定义问卷，显示score方法
     * @param $quiz_id
     * @param $uid
     */

    public function display_score($quiz_id, $uid){
        $sql = "select element_info.element_name, element_info.element_label, element_desc.reg_desc, element_desc.corr_flock, element_desc.flock_desc, element_desc.quiz_id, calc_result.row_score, calc_result.trans_score, element_info.divisor_level from element_info LEFT JOIN element_desc on element_info.divisor_level = element_desc.divisor_level and element_info.element_index = element_desc.element_index and element_desc.quiz_id = element_info.quiz_id LEFT JOIN calc_result on element_info.divisor_level = calc_result.divisor_level and element_info.element_index = calc_result.element_index and calc_result.quiz_id = element_info.quiz_id where element_info.quiz_id = $quiz_id and calc_result.user_id = ".$uid.";";
        $dao = M('calc_result');

        $rs = $dao->query($sql);
        $result = [];
        $k = 0;
        for ($i = 0; $i < count($rs); $i++){
                $result[$k] = [
                    'element_name' => $rs[$i]['element_name'],
                    'element_label' => $rs[$i]['element_label'],
                    'describe_low'=> [
                        'corr_flock' => $rs[$i]['corr_flock'],
                        'flock_desc' => $rs[$i]['flock_desc'],
                        'reg_desc' => $rs[$i]['reg_desc']
                    ],
                    'quiz_id' => $rs[$i]['quiz_id'],
                    'row_score' => $rs[$i]['row_score'],
                    'trans_score' => $rs[$i]['trans_score'],
                    'divisor_level' => $rs[$i]['divisor_level']
                ];

                $k ++;
        }

        //var_dump($result);
        $this->assign("result", $result);
        $this->display("Quiz@CustomQuiz:display_score");
    }


    /**显示个人16pf文件方法（display=1直接显示文件，默认返回结果数组）
     * @param $uid
     * @param int $display
     * @return array
     */
    public function show_report($quiz_id ,$type="score" , $uid)
    {
        if ($type == 'score'){
            $this->display_score($quiz_id, $uid);
        }
        else if($type == 'count'){
            $this->display_count($quiz_id, $uid);
        }
        else{
            echo "unknown type";
        }

    }


    /**显示团队16pf报告方法，display = 1直接显示，默认返回数组
     * @param $oid
     * @param $quiztitle
     */

    public function show_16pf_groupreport($oid =0, $quiz_grouptitle = "")
    {
        $show_warn = 1;
        if (!$oid && !$quiz_grouptitle){
            $uid_list = M()->query("select users.* from users, calc_result where users.uid = calc_result.user_id and users.oid = 0 and calc_result.quiz_id=6 GROUP BY users.uid;");
            $show_warn = 0;
        }
        else{
           // $uid_list = M('orgnizer_userquizlist')->field('uid')->where(['quiztitle' => $quiz_grouptitle , 'oid' => $oid])->select();
            //$uid_list = M('orgnizer_userquizlist')->field('uid')->where(['testgroup_id' => 8 , 'oid' => 8])->select();
            $uid_list = M('orgnizer_userquizlist')->field('uid')->where(['title' => $quiz_grouptitle , 'oid' => $oid])->select();
        }

        $tvalue_list = M('calc_result');
        $rs = [];
        $i = 0;

        foreach($uid_list as $uid){
            $data = $tvalue_list->where(['user_id' => $uid['uid'], 'quiz_id' => 6])->select();
            if (!$oid && !$quiz_grouptitle){

            }
            else{
                $userinfo = D('Orgnizeruserclass')->relation(true)->where(['uid' => $uid['uid']])->select();
            }


            $userinfo = $userinfo[0];

            foreach($data as &$d){
                if ($d['trans_score'] <=0 )
                    $d['trans_score'] = $d['row_score'];//没有转换分则显示原始分
            }

            if ($data){
                $rs[$i]['score_data'] = $data;
                if (!$oid && !$quiz_grouptitle){
                    $rs[$i]['username'] =$uid['username'];
                }
                else{
                    $rs[$i]['username'] =$userinfo['Users']['username'];
                }

                $rs[$i]['class'] = convertClassJson($userinfo['class']);
                $rs[$i]['age'] = $userinfo['Users']['age'];


                //$rs[$i]['sex'] = $userinfo['Users']['sex'];

                if($userinfo['Users']['sex'] == 'male')
                    $rs[$i]['sex'] = '男';
                else
                    $rs[$i]['sex'] = '女';

                $i ++;
            }
        }

        $i = 0;
        $element_info = M("element_info")->where(["quiz_id" => 6])->select();
        $element_name = [];
        foreach($element_info as $e){
            $element_name[$i] = $e['element_name'].$e['element_label'];
            $i++;
        }

        $result_table = A("Quiz/Quiz")->WarnUserList($rs,$oid,"16pf_test", $quiz_grouptitle);

        $this->assign("element_name", $element_name);
        $this->assign('result_table', $result_table);
        $this->assign('title', $quiz_grouptitle);
        $this->assign('show_warn', $show_warn);
        $this->display("Quiz@Pf16:show_16pf_groupreport");


    }



    public function randomAnswer($user_id)
    {
        for($qst_id = 1; $qst_id <= 40; $qst_id++){

            $rs[] = [
                'quiz_id' => 14,
                'qst_id' => $qst_id,
                'selection' => rand(1,4),
                'user_id' => $user_id

            ];

        }


       // var_dump($rs);
        M('user_answer')->addAll($rs);
        if ($this->calcAnswer(14,$user_id))
            echo "$user_id disc random answer success";
        else
            echo "$user_id disc random answer failed";

    }

    public function disc_sample()
    {
        $this->display_count(14, 34);
    }




}